Android setlayout visibility on button click - android

In the following code on click of the button i want to hide the relative layout rl1 and show rl2 but my app crashes down after on button click.What am i doing wrong
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/welcome_1">
<ImageView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="360dp"
android:layout_marginRight="4dp"
android:src="#drawable/button1" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="#drawable/menu_2"
></RelativeLayout>
</RelativeLayout>
Java code
public class mockupsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ImageButton img1 = (ImageButton)findViewById(R.id.button1);
ImageView img1 =(ImageView)findViewById(R.id.button1);
img1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
// Toast.makeText(TouchpointmockupsActivity.this, "test", Toast.LENGTH_SHORT).show();
LinearLayout rl1 = (LinearLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
LinearLayout rl2 = (LinearLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);
}
});
}
}

Change LinearLayout to RelativeLayout in your code. You might be getting a classcastException.
Edit:
Change OnClick method as below
RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);

Related

how to add an ImageButton inside a button in android

i want to make a button in another button ,
for example :
we have 2 buttons : 1.imageButton (100 * 50)dp , 2.button (100 * 100)dp
so my question is how can i put my imageButton inside my button ?
You can use RelativeLayout and just put second ImageButton over first ImageButton.
Update
Or You can use magrin in LinearLayour, for example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true" />
<ImageButton
android:layout_marginLeft="-100dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true" />
</LinearLayout>
Hope this may help you..
You should use Frame Layout for this.
In XML file , do like this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageButton
android:id="#+id/imagebutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="#drawable/buttonsample"/>
</FrameLayout>
And now in java file, declare the instance of Button and ImageButton
ImageButton imagebutton;
Button button;
In onCreate() function of java class, do....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagebutton = (ImageButton)findViewById(R.id.imagebutton);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff
}
});
imagebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff
}
});
}
Set z-elevation property if you want the buttons to overlap and both needs to be visibile. android:elevation="2dp"

Android place view dynamically relative view

Similar to a to do app. I have a top bar that is set by the xml file and at run time I would like to create some textviews which go directly under it. Also there will be a button that adds new text views when clicked.
When I create a new textview from the button how do I make sure that the new Textview stays under the static textviews and/or layouts that I have created. Right now it's not and it's just overlapping.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button btn = (Button) findViewById(R.id.Add_new);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddNewTask(v);
}
});
}
I tried both options to add a new textview and both of them just overalpp and I can't find any documentation on a method such as under/below, most I can find are just alight top and align bottom which wouldn't be useful in this case
public void AddNewTask(View v) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.overallview);
TextView tv= new TextView(this);
tv.setText("TEST");
rl.addView(tv);
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/overallview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.robertli.hustle.MainActivity"
tools:showIn="#layout/activity_main"
android:background="#00095e">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:id="#+id/topbar"
>
<TextView
android:id="#+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Hustle"
android:textSize="25dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="#id/topbar"
android:id="#+id/Add_new"
android:background="#00095e"
android:textColor="#ffffff"
android:clickable="true"
android:textSize="50dp" />
Suggestion : You should take a look at ListView to do this !!! ListView help you dynamically add the textview as well.
if you still want to adding directly to Relativelayout
refer this example:
RelativeLayout relativeLayout = new RelativeLayout(context);
View view = new View(context);
relativeLayout.addView(view);
code to dynamically add alignment in RelativeLayout
RelativeLayout.LayoutParams viewLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
viewLayoutParams.addRule(RelativeLayout.BELOW, R.id.your_view);
take a look at documentation for more
https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Add multiple custom views to layout programmatically

If I for example have empty layout like this:
layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
And some other Layout like this:
layout2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"/>
</LinearLayout>
I would like to add layout2.xml to layout1.xml programmatically. I would need to have multiple different version layout2.xml which I would add when I want. Each one would have different text on TextView and Buttons.
In case of regular Android View I would usually do it similarly like this with addView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
my_linear_layout.addView(tv1);
Button btn2 = new Button(this);
bt2.setText("WORLD");
my_linear_layout.addView(btn2);
}
How can I do it if I don't have something simple like TextView or Button, and if I have my own XML defined View?
Would it be possible to somehow put all the views inside adapter like for a ListView and then get it when needed and just call addView on those Views?
You can inflate the layout2.xml file, edit the texts, and add it to the first layout:
public class MyActivity extends Activity {
private ViewGroup mLinearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
addLayout("This is text 1", "This is first button", "This is second Button");
}
private void addLayout(String textViewText, String buttonText1, String buttonText2) {
View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);
TextView textView = (TextView) layout2.findViewById(R.id.button1);
Button button1 = (Button) layout2.findViewById(R.id.button2);
Button button2 = (Button) layout2.findViewById(R.id.button3);
textView1.setText(textViewText);
button1.setText(buttonText1);
button2.setText(buttonText2);
mLinearLayout.addView(layout2);
}
}
You may want to change android:layout_height of the layout2.xml root view to wrap_content.
If you are using ViewBinding, here is how it would look like for the addLayout function :
MyLayoutBinding binding = MyLayoutBinding.inflate(getLayoutInflater(), mLinearLayout, false);
binding.getTextView1().setText(textViewText);
binding.getButton1().setText(buttonText1);
binding.getButton2().setText(buttonText2);
mLinearLayout.addView(binding.getRoot());
layout1.xml contains ScrollView as parent layout and main LinearLayout as child if no row item more screen size ScrollView handle overflow item with scroll:
layout1.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Use LayoutInflater to add row item to parent LinearLayout:
private LinearLayout my_linear_layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
my_linear_layout = (LinearLayout) findViewById(R.id.my_linear_layout);
for (int i = 1; i <= 5; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.layout2, null);
TextView button1 = (TextView) view.findViewById(R.id.button1);
Button button2 = (Button) view.findViewById(R.id.button2);
TextView button3 = (TextView) view.findViewById(R.id.button3);
button1.setText("HELLO " + i);
button2.setText("HELLO " + i);
button3.setText("HELLO " + i);
my_linear_layout.addView(view);
}
}
Try this method
Add id to LinearLayout 'layout1.xml':
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
In onCreate:
LinearLayout firstlayout = (LinearLayout) findViewById(R.id.firstlayout);
LinearLayout secondlayoout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.layout2, null); // inflating view from xml
TextView btn1 = (TextView) secondlayoout.findViewById(R.id.button1);
btn1.setText("TEST");
firstlayout.addView(secondlayoout);
You can achieve this for view binding by using below.
val layoutBindingView: LayoutBinding =
LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "TextOne"
val layoutBindingView: LayoutBinding =
LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "Text Two"

How to make a 2 buttons that show/hide different layouts? (android, eclipse)

I'am trying to make a blank layout that contains 2 buttons, each views different layout and when one is pressed the other disappears, but I failed I can't even make one button to do that, so if you could help me fix my code that would be great.
P.S: I have only set one button.
Here is my xml:
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/iB2"
android:layout_width="170dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/imageView2" />
<ImageButton
android:id="#+id/iB1"
android:layout_width="170dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/imageView1" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#000000" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone" >
......
</LinearLayout>
</ScrollView>
</LinearLayout>
and here is my java:
public class MainActivity extends Activity implements OnClickListener {
SoundPool sp;
int soundId;
LinearLayout Ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.kh2fm2);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundId = sp.load(kh2fm.this, R.raw.btnclick, 1);
Ll = (LinearLayout)findViewById(R.id.Ll);
ImageButton iB1 = (ImageButton) findViewById(R.id.imageView1);
ImageButton iB2 = (ImageButton) findViewById(R.id.imageView2);
.....
iB1.setOnClickListener(this);
iB2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.iB2:
sp.play(soundId, 1, 1, 0, 0, 1);
if(Ll.getVisibility()==View.VISIBLE)
{
Ll.setVisibility(View.GONE);
}
iB2.setVisibility(View.GONE);
break;
.....
Just a cursory glance would suggest that the L1 view is not visible to begin with, and once you click the button it is just reaffirming this. I assume the button click is working (sound plays, and the iB2 disappears), but L1/L2 aren't swapping.
<LinearLayout
android:id="#+id/Ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone" > <!-- This view is starting off GONE -->
The view is initially set to GONE in the XML. Then when you click iB2, you're affirming that if it were visible, that it will be set to GONE.
if(Ll.getVisibility()==View.VISIBLE) // It is already set to GONE in the XML
{
Ll.setVisibility(View.GONE);
}
Additionally, the findViewById() is not retrieving the buttons, but ImageViews instead:
ImageButton iB1 = (ImageButton) findViewById(R.id.imageView1); // Try R.id.iB1
ImageButton iB2 = (ImageButton) findViewById(R.id.imageView2); // Try R.id.iB2

Insert views in HorizontalScrollView programmatically

I'm making a chat app which looks like this(dummy view):
Use the xmls & activity exactly as they are
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/send"
android:clickable="true" >
<LinearLayout
android:id="#+id/LL_inside_HSV"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageButton
android:id="#+id/send"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I want to add a view to HorizontalScrollView(HSV). I've read that HSV can have only 1 child (mostly LinearLayout) & views must be added to it. This is the view I want to add:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:orientation="vertical"
android:background="#454545" >
<ImageView
android:id="#+id/profilePicture"
android:layout_width="60dp"
android:layout_height="60dp" />
<AutoCompleteTextView
android:layout_width="75dp"
android:layout_height="wrap_content" />
</LinearLayout>
This is my activity:
public class MainActivity extends Activity {
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
LinearLayout lLinsideHSV;
View child;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = inflater.inflate(R.layout.word_element, null, false);
sendIB = (ImageButton) findViewById(R.id.send);
chatMessageHSV = (HorizontalScrollView) findViewById(R.id.chat_message_HSV);
lLinsideHSV = (LinearLayout) findViewById(R.id.LL_inside_HSV);
chatMessageHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT)
.show();
}
});
lLinsideHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "LL", Toast.LENGTH_SHORT)
.show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
lLinsideHSV.addView(child);
}
});
}// end of onCreate
}
NOTE: I want to add views onClick of HSV.
Q-1 : I am unable to trigger the onClick of HSV or the LinearLayout inside it. I tried doing onTouchEvent but that event is triggered 4-5 times on every touch.
Q-2 I wrote the code to insert view inside onClick of button(just to experiment). I am able to insert the view only once. After that it throws an exception saying:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
So how do I insert the views again & again??
Q2: You have to create new child
child = inflater.inflate(R.layout.word_element, null, false);
on every click event. Something like:
#Override
onClick() {
View child = inflater.inflate(...);
lLinsideHSV.addView(child);
}

Categories

Resources