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);
}
Related
I have simple layout with EditText and a button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:focusable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
I don't want EdiText to be editable and want to handle click on complete layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.linearLayout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
}
});
}
}
but it is not working I mean There is no Toast message when I am clicking.
as #Liem Vo said in the comments: you should add android:clickable="false" to the button, and make sure not to add a click listener in java.
becuse the event is handled by the child view.
for the 'EditText' you can call the parents click listener manually:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View parent = (View) v.getParent();
parent.performClick();
}
});
please refer to this question: onClick not triggered on LinearLayout with child
I have a problem adding programmatically buttons to linearLayout, the first button gets added but the others aren't visible.
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(myClass.this);
curButton.setText("" + text);
curButton.setBackgroundResource(R.drawable.tran_mini);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
paginglibrary.addView(curButton);
}
});
this is the xml file where the buttons are being added:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
the first time I click on nextButton the button gets added and is show but the second time I add another button, it doesn't show.
I am sure they are getting added as I checked through the following code:
ViewGroup layout = (ViewGroup)findViewById(R.id.pagingView1);
System.out.println("layout count: "+ layout.getChildCount());
the count shows that the button is getting added but it's not showing.
Any idea what could be the cause?
I tried setting the abscissa of the button to make sure it's not being outside of the view but still the same scenario occurs.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new button"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
The only difference is on the HorizontalScrollView width.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(MainActivity.this);
curButton.setText("generated button");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
ll.addView(curButton);
}
});
I have a navigation drawer in my app which contains a header and some list items. The header has a textview which i want to make clickable but I am not able to do it.
To get the id of this textview I used the following code, since it is in a different layout file compared to the one in the setContentView in onCreate.
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.header, null);
TextView home = (TextView) textEntryView.findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();
}
});
header.xml contains the header of the navigation drawer. It has an item named home. I need to make it clickable. The above code is in the onCreate method.
For me other Answers didn't work.
I have tried the below code.
I know it's too late. Hope this will help some.
What I did to access the view of header.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerview = navigationView.getHeaderView(0);
TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
profilename.setText("your name")
for clicking the views of header, here I have used a linearlayout of headerview
LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.START);
}
});
Or
headerview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
Don't forget to define android:clickable="true" in your TextView xml.
i know its late this is for those who facing the same problem.
place your header layout in the navigation view like this
this is in activity_main.xml
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="-24dp"
app:itemTextColor="#color/black"
app:headerLayout="#layout/layout_header_profile"
app:menu="#menu/nav_menu"/>
create a layout, name it layout_header_profile.xml and put the fill it what ever view you want.
layout_header_profile.xml
<?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="178dp"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:id="#+id/id_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Irfan"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/id_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="Irfan55121#gmail.com"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<ImageView
android:id="#+id/id_profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:src="#mipmap/ic_profile_pic"
/>
</RelativeLayout>
then this header layout file will be in your activity_main.xml only
so in your MainActivity.java you can declare it as you do views from activity_main.xml and perform actions on it, no special code required.
do like this in your onCreate()
TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
tvUserName.setText("My Name");
tvUserName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
}
});
Hope it works Happy coding.
Try like this
navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code here.....
}
});
just add this to your header layout Xml file
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
First fetch header view
View headerView = navigationView.getHeaderView(0);
And then use onClick Listener
headerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// code
}
});
This seems to be a very silly problem but its just not working for me.
I have 2 ImageButtons & a HorizontalScrollView(HSV) on a page. Strangely one of the Image Button(1st one) is not clicking. Also I wanted to make the HSV clickable so I used:
android:clickable="true", which does not work
You can use the xml & activity exactly as I have posted.
This is the xml:
<?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"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/topRL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/free_msgs_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="free msgs left" />
<ImageButton
android:id="#+id/buy_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher"
/>
</RelativeLayout>
<ListView
android:id="#+id/chat_page_listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/send_image_button"
android:clickable="true" />
<ImageButton
android:id="#+id/send_image_button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
This is the activity file:
public class MainActivity extends Activity {
ImageButton buyIB;
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sendIB = (ImageButton) findViewById(
R.id.send_image_button);
chatMessageHSV = (HorizontalScrollView) findViewById(
R.id.chat_message_HSV);
buyIB = (ImageButton) findViewById(R.id.buy_imageButton);
buyIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "buy", Toast.LENGTH_SHORT)
.show();
}
});
chatMessageHSV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT).show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "send", Toast.LENGTH_SHORT).show();
}
});
}// end of onCreate
}
And I cant use button click like
public void onClick(View view)
{}
because the actual problem is containing a drawer layout where I'm using fragments & there this method doesn't work.
The problem in the ImageButton doesn't respond for you click because the ListView will respond first for the click because it above the ImageButton try to re order it , when i remove it the ImageButton respond for the click
about the HorizontallScrollView i found the solution to use the Touch Event instead of Click try this code
chatMessageHSV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(ImageButtonActivity.this, "hsv",
Toast.LENGTH_SHORT).show();
}else if (event.getAction() == MotionEvent.ACTION_UP){
// here code if the touch up
}
return false;
}
});
feed me back
The ImageButton not clicking problem can be solved by adding to ListView these 2 lines:
android:layout_below="#+id/topRL"
android:layout_above="#+id/bottomRL"
I have a simple vertical LinearLayout in which there is a button on the top and a Listview below it. What I want is that when the button is pressed, it hides (using View.GONE) and the empty space generated by it is filled by ListView. But after all the efforts, I have not been able to implement it.
I tried invalidate() and onDraw(), I tried giving layout_weight=1 to the ListView, I tried forceLayout(), requestLayout() methods as well but none of them worked.
The strange thing is that if there is EditText or TextView or any other component instead of ListView, its working fine i.e. Edittext etc. go and grab the empty space generated by hiding the Button. But in case of ListView , its not happening.
This thing is perfectly working in my emulator
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new ProgressTask().execute();
btn1.setVisibility(8);
}
})
XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#+id/itemlist"
android:layout_below="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
check this......
public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
Button checkBtn;
String[] temp={"item 1","item 2","item 3"};
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getListView().setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, temp));
checkBtn = (Button) findViewById(R.id.button1);
checkBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
checkBtn.setVisibility(View.GONE);
}});
} }
xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check connetion" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollingCache="false" >
</ListView>
</LinearLayout>