how to use viewswitcher with same id's in both layouts - android

I am using view switcher in my project.
In my xml I have created 2 layouts with all same Id's.
After I switch my view I can not switch to previous view because I am using same ID's in both layouts.
Now how can I use one listener in java code for both layouts in view switcher.
I dont want to create an another id and create another listener to switch again.
My xml is as below.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Switch" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Switch" />
</RelativeLayout>
</ViewSwitcher>
My java code is as follows
final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
Button btn = (Button) findViewById(R.id.switchBtn);
btn.setOnClickListener(new OnClickListener() {
private boolean switchCheck;
public void onClick(View v) {
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
if (!switchCheck) {
switcher.showNext();
switchCheck = true;
} else {
switcher.showPrevious();
switchCheck = false;
}
}
});
Please help..

Crete different ids for parent relative layout in xml.
And then in java file check which relative layout button is used.
using method
step 1)
RelativeLayout r1 = RelativeLayout findviewbyId(R.id.rl1)
RelativeLayout r2 = RelativeLayout findviewbyId(R.id.rl2)
step2)
if button.getParent equals to R1 then ...
else .....

Just try to change ur text in any of the button or textView .may be ur view is switching but because the code for both the layouts is exactly same u r not able to differentiate.

Related

Removing Views dynamically

I search a lot for this, but I can't solve it. I have LinearLayout with two textviews and a button (to add others LinearLayout) like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText
android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/add_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:layout_margin="#dimen/boxes_margin"
app:icon="#drawable/ic_add_ingr_btn"
/>
</LinearLayout>
The layout added is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
xmlns:android="http://schemas.android.com/apk/res/android" />
<ImageButton
android:id="#+id/remove_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:src="#drawable/ic_remove_ing_qnt"/>
</LinearLayout>
In the Activity, I created a method to add new Layouts (and it works), and inside of it a method to delete the corresponding Layout with the Delete Button. Delete Button works only one time and only if it's in the first layout added. Here's the code:
add_ingredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.ingredient_quantity_layout, null);
// Add the new row before the add field button.
parentIngredientLayout.addView(rowView);
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
removeChildIngredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parentIngredientLayout.removeView((View) v.getParent());
}
});
}
});
You have to set the OnClickListenerevent on the ImageButton from the last inserted layout.
To do that, just change this line
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
to
ImageButton removeChildIngredient = rowView.findViewById(R.id.remove_ingredient_btn);
rowView.findViewById search the ImageButton on the last inserted layout and not the whole layout which contains other layouts.
In this line :
parentIngredientLayout.removeView((View)
v.getParent());
You are trying to remove the parent of the removeChildIngredient ImageButton, which is probably parentIngredientLayout, which i think is not what you want,
You could try this :
parentIngredientLayout.
removeView(rowView);
But in your implementation, you could encounter issues when you will add multiples ingredients, because you are setting a new onClickListener to each new ingredient, and The ImageButton will only delete the last one you have added(the last onClickListener set),
Instead, you could use a List/RecyclerView or search for another implementation, eg. put the ImageButton inside the Inflated Layout so you'll have one Remove Button in each Layout you add,
Then you should replace the findViewById of your ImageButton by rowView.findViewById
And this line should stay unchanged
parentIngredientLayout.removeView((View) v.getParent());

Incorrect alignment creating button in Java vs XML

I have this XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_bar"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
<Button
android:id="#+id/action_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
</LinearLayout>
It´s just a buttonBar with two borderless buttons. It works. However, I don't want that, I need to inflate these buttons from a JSONArray. So I did this:
for (int b = 0; b < buttons.length(); b++) {
final JSONObject button = buttons.getJSONObject(b);
LinearLayout buttonBar = (LinearLayout) child.findViewById(R.id.button_bar);
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
Button action = (Button) buttonChild.findViewById(R.id.action_button);
action.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
action.setText(button.getString("descricao"));
action.setTextColor(Color.parseColor(button.getString("text_color")));
buttonBar.addView(buttonChild);
}
It works too, but the buttons get a left alignment. I want they justified.
Why it works when I let them fixed but not when I inflate them?
OBS: The "button_bar" is A XML just with a LinearLayout and the "ActionButton" is just a XML with a Button.
This is the root of your problem:
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
If you don't provide the parent view to the inflate() method, any LayoutParams attributes (e.g. layout_gravity) will be discarded since the parent is the one to interpret those attributes.
You can fix this by changing it to:
View buttonChild = getLayoutInflater().inflate(
R.layout.flat_button, buttonBar, false);
Which will give it the parent you're attaching it to, but not attach it to the hierarchy yet (you do that below with addView()).

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

Android scrollTo(0,0) action on click

Hello I am really new on android development and I would like to ask a simple question.
I have a button (buttonR2C1).
When I click the buttonR2C1 I change the text of my textView (the textview is included inside a scrollView).The problem is that the position of the scrollView remains the same.
I would like to add an action that brings my ScrollView in position 0,0 by using the scrollTo action.
Can anyone help me achieve this?
.java
final TextView tv2 = (TextView) findViewById(R.id.mainL1R2);
final Button button1 = (Button) findViewById(R.id.buttonR2C1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
tv2.setText(getResources().getString(R.string.L1R2C1));
}
});
.xml
<Button
android:id="#+id/buttonR2C1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capitolo 1"
android:textSize="18sp" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical" >
<TextView
android:id="#+id/mainL1R2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fontFamily="CustomText"
android:gravity="left"
android:scrollHorizontally="true"
android:text="#string/L1R2C1"
android:textSize="20sp"
android:textStyle="normal" />
You have to use the scrollTo method of the ScrollView, not the TextView, like
final ScrollView scr = (ScrollView) findViewById(R.id.scrollView1);
scr.scrollTo(x, y);
To scroll exactly to your TextView, you'll have to get the position of the TextView.
Have a look at this answer.

setContentView in OnLongClick Listener

i got a problem with setContentView which throws me a null pointer.
In my mainFrame i got a small SurfaceView which takes about 50% of the screen.
I wanted to implement a method which makes that SurfaceView go Full Screen if you long clicked it. I did it like this:
final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout);
frame.setOnLongClickListener(new OnLongClickListener() {
boolean clicked = false;
#Override
public boolean onLongClick(View v) {
if (clicked){
clicked = false;
(MyActivity.this).setContentView(R.layout.main);
} else {
clicked = true;
(MyActivity.this).setContentView(R.layout.fullScreen);
}
return true;
}
});
Can someone help me how to fix this problem?
Greetings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical"
android:weightSum="1"
android:background="#drawable/background_new" >
<TextView
android:text="#string/measures"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/text_measures"
android:textColor="#ff444444"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
/>
<ImageButton
android:contentDescription="#string/desc_edge"
android:layout_toRightOf="#id/text_measures"
android:id="#+id/edges_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edges_black"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
android:onClick="edge_handler"
android:background="#null"
/>
<FrameLayout
android:background="#xml/border"
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="35dp"
android:layout_marginRight="200dp"
android:layout_marginTop="80dp"
android:layout_below="#id/text_measures"
>
</RelativeLayout>
I know it looks like a FrameLayout but dont get fooled it was done by someone else and it is actually a SurfaceView.
Just change the layout parameters of the SurfaceView onLong click of frame. No need to setContentView again.
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
surfaceView.setLayoutParams(lp);

Categories

Resources