Vyan Garcia Vyan Garcia - 3 months ago 56
Android Question

Android: Navigation Drawer. After log in, username will appear

I used android studio, mysql and my database uploaded in webhost(online). What I want my system to do is after logging in, the username or name that they log in will show. I've try some tutorial but error. Help me, here's my code.

Login Activity Code

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

public static final String username = "Username";

public static final String password = "password";

private static final String LOGIN_URL = "http://teampakers1234.comli.com/login.php";

private EditText editTextUserName;
private EditText editTextPassword;

private Button buttonLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);

editTextUserName = (EditText) findViewById(R.id.username);
editTextPassword = (EditText) findViewById(R.id.password);

buttonLogin = (Button) findViewById(R.id.buttonUserLogin);

buttonLogin.setOnClickListener(this);
}


private void login(){
String username = editTextUserName.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
userLogin(username,password);
}

private void userLogin(final String username, final String password){
class UserLoginClass extends AsyncTask<String,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginActivity.this,"Please Wait",null,true,true);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this,s,Toast.LENGTH_LONG).show();
}
}

@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("username",params[0]);
data.put("password",params[1]);

RegisterUserClass ruc = new RegisterUserClass();

String result = ruc.sendPostRequest(LOGIN_URL,data);

return result;
}
}

UserLoginClass ulc = new UserLoginClass();
ulc.execute(username,password);
}


@Override
public void onClick(View v) {
if(v == buttonLogin){
login();
}
}


Main Activity (navigation design)

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView3);

Intent intent = getIntent();

String username = intent.getStringExtra(MainActivity.USER_NAME);

textView.setText("Welcome"+username);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.nav_edit) {

} else if (id == R.id.nav_friend) {

} else if (id == R.id.nav_group) {

} else if (id == R.id.nav_event) {

} else if (id == R.id.nav_about) {

} else if (id == R.id.nav_out) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}


It's like a social network. Can you please suggest any tutorial according add friend, and post status? thank you in advance.

Image Here.

DsD DsD
Answer

Another Approach:

For better accessibility throughout the application, you can save the username in Shared Preferences after successful login and just retrieve the name from Shared Preferences when and on any screen you want to show.

Basic share preferences code:

Add:

// In your login activity.........

SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("USER_NAME", username); //username the user has entered
editor.commit();

Retrieve:

SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
String username = sp.getString("USER_NAME", "Unknown");

//...in your Main Activity -> Navigation Drawer code:
textView.setText("Welcome"+username);