I want to add text under a image button, which are also clickable, and will be redirected to the same activity as the image button. basically it's like the app list in any android phone, e.g.
If you have your ImageButton declared in XML, then just put it into a LinearLayout which also contains a TextView and set the onClickListener on the LinearLayout. The structure would be like
<LinearLayout
... > <!-- This is the LinearLayout for the xml document -->
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton
... />
<TextView
... />
</LinearLayout>
</LinearLayout>
And then in your java:
LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you're adding each ImageButton dynamically via java code, then it will still maintain the same structure. Let me know if I need to add anything.
setAlpha and do the below
<Button
android:id="#+id/comparePack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:onClick="compareButtonClick"
android:text="#string/compare"
android:drawableTop="#drawable/compare1edit"
android:gravity="left|center_vertical" />
if you want to create this king of list use gridview
and in getview method inflate customlayout with imageview and textview
Also you can set onClickListener to TextView
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton android:id="#+id/btn1"
... />
<TextView android:id="#+id/tv1"
... />
</LinearLayout>
java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
Related
i want to have two relativelayout in first relativelayout have map and in second relativelayout i have the list..,i want on starting only layout with map will be visible on screen with a button,,when i click on button then layout with listview get open from right side with new new button on the top of it,,and prevoius button get hide.and screen get divided with two parts with different layouts..i have done some thing but from starting onward m getting half half screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ListView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RelativeLayout
android:id="#+id/rl_ListView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="#+id/getdirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" >
</fragment>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_ListView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible" >
<Button
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
MainActivity
show = (TextView)findViewById(R.id.getdirection);
show1 = (TextView)findViewById(R.id.hide);
rt = (RelativeLayout)findViewById(R.id.rl_ListView2);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.INVISIBLE)
{
rt.setVisibility(View.VISIBLE);
}
show.setVisibility(View.INVISIBLE);
}
});
show1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.VISIBLE)
{
rt.setVisibility(View.INVISIBLE);
}
show1.setVisibility(View.INVISIBLE);
}
});
Try this:
You are getting layout1 in half screen from starting due to weight property. You can try not giving weight in starting and give it programatically on button click.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button1.setVisibility(View.GONE); //hide old button
layout2.setVisibility(View.VISIBLE); //show layout2
//set Relativelayout 1 to half screen
RelativaLayout.LayoutParams params = layout1.getLayoutParams();
params.weight = 0.5;
layout1.setLayoutParams(params);
}
});
Hope this helps.
Set visibility as Gone instead of Invisible
button.setOnClickListener(this);
public void onClick(View v) {
layoutid.setVisibility(View.GONE);
}
You can try with isShown() as suggested by Amiya
<b>
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(button1.isShown()) {
// Your_Staff
}
else{
// Your_Staff
}
}
});
</b>
From my view you did not set the orientation in the LinearLayout to be vertical for the two RelativeLayouts. I also suggests you put the map and Button in a FrameLayout as well instead of RelativeLayout.
I allow the App's User to change the ImageButton's pictures from the drawable folders?
It's possible?
I want associate this action after onLongClickListener,
I put in the Drawable folder about 3 or 4 pictures(png) and the User can choose one for its ImageButton.
Yes, you can. On onLongClickListener click, you can pop up the option and then put a switch statement and put the following for each of the cases:
aButton.setImageResource(R.drawable.image2);
Here is the more detailed answer:
Put the following at the bottom of the layout (just before the last closing layout tag)
<FrameLayout
android:id="#+id/imagebuttonselectorlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imgButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1" />
<ImageButton
android:id="#+id/imgButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image2" />
</LinearLayout>
</FrameLayout>
Then, in the java class file, add the following lines:
FrameLayout mFrameLayout;
ImageButton mImageButton1;
ImageButton mImageButton2;
mFrameLayout = (FrameLayout)findViewById(R.id.imagebuttonselectorlayout);
mImageButton1 = (ImageButton)findViewById(R.id.imgButton1);
mImageButton2 = (ImageButton)findViewById(R.id.imgButton2);
For the onLongClick of the main image button
mImageButton1.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mFrameLayout.setVisibility(View.VISIBLE);
return true;
}
});
Add the following lines in the same file to complete the functionality:
mImageButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image1);
mFrameLayout.setVisibility(View.GONE);
}
});
mImageButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image2);
mFrameLayout.setVisibility(View.GONE);
}
});
I need to add a textView and ImageView at the bottom of the screen
I need to hide the textView and show it again programmatically
here I have this image to explain the Question and how the application should behave
the left rectangle desplays the TextView shown and the second for Hiding Textview:
I tried the code below to do that:
1- I added click listener to ImageView that hide the TextView and Change the LayoutParams of ImageView
2- On Clicking it again it will show the TextView and align the ImageView above it
but the problem is that when I run the code the ImageView is stuck to
the top of the screen
I'm trying to change the LayoutParams of a view pragmatically using this code :
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#id/tv1" />
</RelativeLayout>
and the class MainActivity.java :
public class MainActivity extends Activity {
TextView tv;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.img);
tv = (TextView) findViewById(R.id.tv1);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
if(tv.getVisibility() == MyNavigationBar.GONE){
tv.setVisibility(MyNavigationBar.VISIBLE);
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
image.setLayoutParams(imageParams);
} else {
tv.setVisibility(MyNavigationBar.GONE);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
image.setLayoutParams(imageParams);
}
}
});
}
}
I think the problem is that the function :
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
but its not work
I also tried to set an Id manually to the textView like:
tv.setId(1);
and changing the previous to :
imageParams.addRule(RelativeLayout.ABOVE, tv1.getId());
Also doesn't work for me !!
I wonder that whether the problem in my code or in the android SDK !!
Seems layout_alignWithParentIfMissing would be much easier:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#+id/tv1"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
And only switch visibility of tv1 in your code:
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(tv.getVisibility() == View.GONE){
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
})
Click on text view is not working
My xml is
<TextView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center"
android:textColor="#color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>
and My Activity code is
public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}
But it is not working.I am not able to click that link.please guide me
Well, I use the following code to make a TextView clickable.
First, add this at your activity.xml, to make TextView clikable:
<TextView
android:id=android:id="#+id/button2" <!-- id to get this TextView -->
(...)
android:onClick="onClick" <!-- Add the function onClick() -->
android:clickable="true" <!-- Set the boolean clickable to true -->
/>
Then, at your MainActivity.java, you add:
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView by id
textview = (TextView) findViewById(R.id.button2);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO when clicked on your link(TextView)
}
});
}
As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.
The android developer reference to TextView:
https://developer.android.com/reference/android/widget/TextView.html
The similar question in Stack Overflow about how to make a links in a clickable that might be helpful:
How do I make links in a TextView clickable?
You may use like this
<TextView
android:id="#+id/topPaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:text="#string/topPaid"
android:textColor="#000000"
android:textColorLink="#33CC33" />
and At activity
TextView topPaid = (TextView) findViewById(R.id.topPaid);
Linkify.addLinks(topPaid, Linkify.ALL);
topPaid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//write ur logic
}
}
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b> Text with a " + "link " + "Created in the Java source code using HTML."));
XML Code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("google "));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your XML as the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
In your attached activity
public void onTextViewPressed(View view) {
...
}
I have set linearlayer to become clickable and want it to act like a button and start a new activity. However i got error. Here are part of the .xml
<LinearLayout
android:id="#+id/llproduct1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:clickable="true">
<ImageView .... />
<TextView .... />
</LinearLayout>
This is the .java
Button bProduct1 = (Button) findViewById(R.id.llproduct1);
bProduct1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.testing.PRODUCTDESCRIPTION"));
}
What went wrong?
Button bProduct1 = (Button) findViewById(R.id.llproduct1);
you cannot cast your LinearLayout to a button. But you can do:
LinearLayout bProduct1 = (LinearLayout) findViewById(R.id.llproduct1);
bProduct1.setOnClickListener(...)
see this for reference
It is wrong class casting in "bProduct1 = (Button) findViewById(R.id.llproduct1); "
'llproduct1' is LinearLayout!! not a Button.
therefore the java code cause ClassCastException.
onClick method is declared in View class.
and both of LinearLayout and Button are inheriting View class.
so why don't you fix the code below.
View bProduct1 = findViewById(R.id.llproduct1);
bProduct1.setOnClickListener(......);