Switching layout whit onBackPressed() - java

I want to switch between layouts using the physical back button. I found this:
#Override
public void onBackPressed() {
RelativeLayout layout2 = (RelativeLayout)findViewById(R.id.layout2);// check if layout2 is open
if(layout2.getVisibility() == View.VISIBLE){
setContentView(R.layout.main);
return;
}else{
super.onBackPressed();
}
}
Is the checking part correct?

Make RelativeLayout decalre globally so you can access in OnBack() method
RelativeLayout layout2
this line you have to put in the OnCreate() method
layout2 = (RelativeLayout)findViewById(R.id.layout2);//
and finally check visibility of layout
#Override
public void onBackPressed() {
if(layout2.getVisibility() == View.VISIBLE){
setContentView(R.layout.main);
return;
}else{
super.onBackPressed();
}
}
enjoy coding............

Related

startactivity creates two instances

for my android app, I use a button to go to the next Activity.
the problem is when I touch the button on the screen one instance of the activity is created but when
I use performClick() method to click the button programmatically, it creates two instances of the activity. ( performClick() is called from a callback method).
I used the CLEAR_TOP FLAG but it seems to break the back button.
Any idea how to solve this problem ?
this is what my code looks like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
}
private void A_callback_method(){
if (some_condition_to_launch_activity){
btn.performClick();
}
}
Just a simple trick. create function
private void function_name(){
startActivity(new Intent(Activity1.this, Activity2.class));
}
Then on the button onClickListener
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
function_name();
}
});
and also inside A_callback_method
private void A_callback_method(){
if (some_condition_to_launch_activity){
function_name();
}
}
hope this will solve your problems.
I succesfully did a work around to solve this problem by adding a boolean variable intialise it to false in the onResume() method and then did the following:
private boolean clicked_btn;
private void A_callback_method(){
if ((some_condition_to_launch_activity)&&!clicked_btn){
clicked_btn=true;
btn.performClick();
}
}

how to get back hamburg icon when set back icon?

how to get back hamburg icon when set back icon?
hen I am implementing change toggle icon when fragment on activity ,its set to back icon but need when its back the fragment then set it to again hamburg icon for open drawer of the activity,
this is my code
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
toolbar.setNavigationIcon(R.drawable.ic_action_navigation_arrow_back);
//drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);// show back button
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
} else {
//show hamburger
///drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(true);
toolbar.setNavigationIcon(R.drawable.icon1);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
//drawerFragment.mDrawerToggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
}
});
}
}
});
If you are using fragment to show content and have the single toolbar inside host activity, you could manage your toolbar.
#Override
public void onResume() {
super.onResume();
ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
actionBar.setTitle("First Fragment");
actionBar.setIcon(R.drawable.back_icon);
}
Do not forget to call
setSupportActionBar(toolbar)

Only click Checkbox in clickable Relative Layout

I'm trying to design a layout with a checkbox inside of a relative layout. The relative layout is clickable and works as it is supposed to, but when the checkbox is click, it runs the checkbox code, and the relative view code.
Is there anyway to ignore the relative layout's OnClickListener while running the checkbox?
RelativeLayout holder = new RelativeLayout(context);
contactHolder.setId(View.generateViewId());
TextView name = new TextView(context);
final CheckBox checkBox = new CheckBox(context);
checkBox.setId(View.generateViewId());
name.setText(contact.getName());
name.setId(View.generateViewId());
holder.addView(name);
holder.addView(checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
userView.addView(holder);
}
use this in Relativelayout
android:clickable="true"
Remove the listener from the check box. Then change the logic in RelativeLayout's listener to act based on the state of the check box.

How to make a textview show after the button click how to hide the textview again by clicking on the same button in android java

How to make a textview show after the button click how to hide the textview again by clicking on the same button in android java.Should i have to try if.else structure?
You can use a FLAG of some sort to make this happen
int i = 0;
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(i%2==0){
textView.setVisibility(View.INVISIBLE);
}
else{
textView.setVisibility(View.VISIBLE);
}
i++;
}
}
Initially make the textview disable after clicking the button make it enable,again after clicking the button disable the button.
Use textview.setVisibility() to achieve this.
In button.setOnClickListner() method put this code:
if(textview.getVisibility() == View.GONE){
textview.setVisibility(View.Visible);
}else{
textview.setVisibility(View.GONE);
}
and in layout xml set default visibility of textview as you desire that can be visible or gone or invisible
yourButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(textview.getVisibility() == View.GONE)
{
textview.setVisibility(View.Visible);
}
else
{
textview.setVisibility(View.GONE);
}
}
}

Null PointerExeption ALL the time?

Why is mainB_news allways null? (in the method onBackPressed())
In onCreate I set a value to the button!!! :(
PS: with findViewById() I get the same error...
public class MainActivity extends Activity {
private Button mainB_news;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainB_news = (Button) findViewById(R.id.mainB_news);
mainB_news.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.news);
}
});
}
#Override
public void onBackPressed() {
// check if page 2 is open
if (mainB_news != null && mainB_news.isShown()){
setContentView(R.layout.main); // open main view again
return;
}else
super.onBackPressed(); // allows standard use of backbutton for page 1
}
}
THANKS a lot!
Because you change contentView on button click. mainB_news doesn't exist after contentView changing. You shouldn't use setContentView() in this manner. Consider using another activity for showing news.
Because it is not defined in res/layout/main.xml ?

Categories