I have got one simple problem, my app contains 52 buttons but i cant display all buttons. I need suggest have can i solve this trouble the best as I can.
At the moment i have buttons like that:
http://prntscr.com/482kjw
but this solve is untransparent and i want one button make two functionaly one on click and second on long clik or something that, but i havent got idea how..
and now my question is: I need recommendations for the solution of my problem or some tutorial how to display two images on one button, Gridview.
you can make a layout of two images in one on a linearLayout with orientation:Horizontal or
To add two pics on one button, try this
android:drawableEnd="#drawable/ic_action_done1"
android:drawableTop="#drawable/ic_action_done2"
and so forth.. this puts two images-(#drawable/ic_action_done1 & #drawable/ic_action_done2)- on one button
and speaking of the functionality of the buttons, from my understanding to your question, you need a click function and a long press function, well you can use one button for to achieve these..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
return false;
}
});
maybe it might help you.
SAMPLE OR TUTORIAL ON WHAT YOU ASKED
The name is button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/registerButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="8dp"
android:drawableLeft="#drawable/checkmark"
android:drawableRight="#drawable/ic_launcher"
android:text="#string/register"
android:textColor="#ffffff"
android:textSize="22sp" />
button_layout is the xml_file you will inflate..in the link you gave me the button_xml was written programatically in the baseAdapter which was
imageView imageView;
imageView = new ImageView(mContext);
you can also do yours programmatically in this way
Button button = (Button)getLayoutInflater().inflate(R.layout.button_layout, null);
it seems you are new in android. so check these sites, it might give you a lucid idea of what i mean..
http://www.learn2crack.com/2014/01/android-custom-gridview.html
http://www.mkyong.com/android/android-gridview-example/
http://www.tutorialspoint.com/android/android_grid_view.htm
http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76&aaid=100
http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android
Related
I am using an ImageView as a NEXT button in my Android app which is responsible for loading the next page of results into the current activity. However, despite that I bind a click listener to it, I cannot seem to capture click events on this ImageView. Here is the XML:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="left"
android:orientation="horizontal">
<ImageView
android:id="#+id/listBackIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back_icon"/>
<TextView
android:id="#+id/listBackLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:textSize="16dip"/>
</LinearLayout>
And here is the relevant Java code:
ImageView forwardIconView = (ImageView)findViewById(R.id.listBackIcon);
// not sure if necessary; doesn't fix it anyway
forwardIconView.setClickable(true);
forwardIconView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
++pageNumber;
try {
params.put("page", pageNumber);
} catch (JSONException e) {
// do something
}
ConnectionTask task = new ConnectionTask();
task.execute(new String[0]);
}
});
I spent about an hour researching this on Stack Overflow. I found a few places which claimed that ImageView could directly be made clickable, but most things recommended workarounds using other types of widgets.
Does anything about my layout/code stand out as being a culprit for this behavior?
Update:
I also tried binding a click listener to the TextView at the same level as the ImageView and this too failed to capture clicks. So now I am suspecting that the views are being masked by something. Perhaps something is capturing the clicks instead of the views.
I would set it up like this:
private ImageView nextButton;
nextButton = (ImageView)view.findViewById(R.id.back_button);
Util.loadImage(getActivity(),R.drawable.some_image,nextButton); //Here i would load the image, but i see you do it in XML.
nextButton.setOnClickListener(nextButtonListener);
nextButton.setEnabled(true);
View.OnClickListener nextButtonListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v(TAG, "ImageView has been clicked. do something.");
}
};
This works for me.
Why not use android:drawableLeft attribute for the textview instead of using imageView​ and textview both in a linearlayout .
<ImageView
android:id="#+id/listBackIcon"
...
android:clickable="true"
Or you can try overriding onTouchListener with ACTION_DOWN event filter, not onClickListener. Also check for parrents with android:clickable="false", they could block childs for click events.
What seemed to work for me was the accepted answer from this SO question, which suggests adding the following the every child element of the LinearLayout which I pasted in my question:
android:duplicateParentState="true"
I don't know exactly what was happening, but it appears the click events were not making it down to the TextView and ImageView. Strangely, the click events were reaching a Button, when I added one for debugging purposes. If someone has more insight into what actually happened, leave a comment and this answer can be updated.
I am developing an Android app that has an ImageButton in it, and I need to add an event handler to it. In my attempt to do this, I have added a new function to the .java file that Android Studio creates when you make new project that looks like this:
public void tapImageButton(ImageButton myImgBtn) {
// Code that does stuff will come later on.
}
After doing this, trying to set the onClick proerty does not show the function, and trying to enter it manually causes the app to crash when I test it.
You must have View as a parameter of method that you wish to call, while bind click event through layout file.
Try this:
public void tapImageButton(View view) {
// Code that does stuff will come later on.
Toast.makeText(this, "clicked !!", Toast.LENGTH_SHORT).show();
}
In xml:
...
android:onClick="tapImageButton"
...
I am not very familiar with android studio but when i tried making a button and having it do something on click i first added the android:onClick = "some_method_name" to the XML that makes the design of the app. I first added the name in the onClick and then i clicked on the name. When the little light bulb icon shows up the arrow next to it give the option of creating a method with that method name that you just wrote. once you create it then you add the code of whatever you want to happen when you click. Hope it works.
For Example, This is the XML:
<Button
android:id="#+id/playButton"
android:text="#string/play"
android:textSize="25sp"
android:background="#0099FF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:onClick="startGame" />
This is the Java:
public void startGame(View view) {
Intent intent = new Intent(this,GameActivity.class);
startActivity(intent);
}
I am taking reference from TextJustify-Android. I am implementing option 2 in above link. When I run my app on emulator text appears one word in one line, next word in next line and so on. I dont know whats wrong in my code. Please help me. Thanks.
Activity class code-
textView1 = (TextView) findViewById (R.id.textView1);
textView1.setMovementMethod(new ScrollingMovementMethod());
textView1.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener()
{
boolean isJustified = false;
#Override
public boolean onPreDraw()
{
if(!isJustified)
{
TextJustifyUtils.run(textView1,0);
isJustified = true;
}
return true;
}
});
Xml code-
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:text="#string/his"
android:textColor="#FFFFFF"/>
</LinearLayout>
And I am implementing TextJustifyUtils class in my app as suggested in above link.
I have made one change In that link given TextJustifyUtils.run(textView1); and In my code eclipse suggest me to change in TextJustifyUtils.run(textView1,0);. Is anything wrong with this?
Update:
In TextJustifyUtils I change public static void justify(TextView textView) into public static void run(TextView textView) as commented by the author there and TextJustifyUtils.run(textView1,0); into TextJustifyUtils.run(textView1); in Activity class. But the output is same as I type in my textView i.e text without justification.
If some one following the above link to justify text please choose option 1. Its work fine. And if you have any problem. Ask from author. I think he always happy to help you nice guy. As he helps me so much. And option 1 working with minor changes.
I have read a number of tutorials on this topic but not able to make it working. Can anyone please help me with the following
a) A home screen widget which has a button, Imageview and a TextView
b) The textview updates periodically via a service
c) On Click of the button the Image Changes.
Can anyone please help with a sample code or point to codes which do this functionality
check out this book, it explains very clear how to make a widget "silent mode toggle"
http://iit.qau.edu.pk/books/Android.Application.Development.for.For.Dummies.pdf
a) In your xml, the following will be the code.
<Button
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/go" />
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#string/welcome" />
Of course, you need to arrange them according to your fancy.
b) Updating the text view is no big deal. Getting the text from the source is what is important. If it is from a web page, you could refer to usage of JSON. If it is from a database, then try SQLite. It all depends on your necessity.
Lets assume that you get your text. Your code for updation will be:
TextView t =(TextView) findViewById(R.id.tv);
t.setText(your_string);
To do it periodically you can use a parallel threading concept such as a runnable.
c) Set up an on Click Listener for this purpose.
Button b1;
b1 = (Button) findViewById(R.id.b);
b1.setOnClickListener(button_func);
This comes in your onCreate method.
View.OnClickListener button_func = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
image = (ImageView) findViewById(R.id.iv);
iv.setImageResource(R.id.new_image);
}
};
Hope that you find this useful
I'm trying to set up a "close to start" button in a game. The game takes the user from view to view like a maze. The user could be on any view, but I want a way to get them back to the start screen. Is there a way to find and return the ID of the current view for my findViewByID? I've found a I've tried the following code in various forms:
OnClickListener closetomain = new OnClickListener() {
#Override
public void onClick(View v) {
int currentid = v.findFocus().getId();
RelativeLayout hideme = (RelativeLayout)findViewById(currentid);
hideme.setVisibility(View.GONE);
setContentView(R.layout.main);
// RelativeLayout showme = (RelativeLayout)findViewById(R.id.startLayout);
// showme.setVisibility(View.VISIBLE);
}
};
Okay, it turns out I should have given each close button the same ID and used theisenp's suggestion for the simplest code (well, that I could understand). It also turns out that I should have started by putting each level/layout in its own activity. You live and you learn I guess. Below is my XML and java. It may not be the elegant solution but I'm not sure I care all that much as long as it works.
<ImageButton android:id="#+id/closeButton"
android:layout_height="wrap_content"
android:background="#drawable/close"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:onClick="closetomain">
</ImageButton>
And here's my Java:
public void closetomain(View view) {
switch(view.getId()) {
case R.id.closeButton:
setContentView(R.layout.main);
break;
}
}
Why do you need to retrieve the id of the current view? Is it just so that you can set it's visibility to GONE? If so, there are probably better ways of implementing that same functionality.
Instead of just changing the layout with setContentView(), it would probably be a better idea to have the Start Screen be its own separate activity. When you are in any of the "maze views" you could simply use an intent to start your home screen activity like this
Intent startScreenIntent = new Intent(this, StartScreen.Class);
startActivity(startScreenIntent);
Then you won't have to worry about finding id's or changing visibilities, plus the code is cleaner, because it separates the code for your Maze levels and your Start Menu.