I given Long Click on Linear layout.Code is given below .Its working on Many devices.but not working on few devices Like Samsung Galaxy Tab E, SM-T561 .
activity_main layout,
<LinearLayout
android:id="#+id/ll_accept"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#drawable/ic_accept_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/new_accept"
android:textColor="#color/new_accept_color"
android:textSize="#dimen/new_job_accept_reject_txt_size" />
</LinearLayout>
MainActivity() class,
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {
...........
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.....................
llAccept = (LinearLayout) findViewById(R.id.ll_accept);
llAccept.setOnLongClickListener(this);
......................
}
#Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.ll_accept:
//do your need
break;
}
}
}
longClicklistener need to return boolean, false if a click listener has to still execute after the long click and true if there are no further actions needed
Another solution could be adding clickable, focusable and other attributes like those to the LinearLayout.
Related
I have a fragment inside the main activity.The fragment contains an image view.My aim is to divide the image view into 3 different buttons.So ,i set up a linear layout which overlaps the image view and it contains 3 clickable views with equal gravity.
In the fragment i implements OnClickListener ,as this-
public class FragmentHomeMenu extends Fragment implements OnClickListener {
View view;
Context activity_context;
View button_search_1;
View button_search_2;
View button_search_3;
...plenty of other stuff
public FragmentHomeMenu() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home_menu, container, false);
activity_context = getActivity().getApplicationContext();
button_search_1 = (View) view
.findViewById(R.id.button_search_1);
button_search_1.setOnClickListener(this);
button_search_2 = (View) view.findViewById(R.id.button_search_2);
button_search_2.setOnClickListener(this);
button_search_3 = (View) view.findViewById(R.id.button_search_3);
button_search_3.setOnClickListener(this);
....plenty of other stuff
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activity_context = activity.getApplicationContext();
}
#Override
public void onClick(View v) {
Log.w("", "some thing was clicked");
switch (v.getId()) {
case R.id.button_search_2:
dostuff1();//stuff1 is not happening in android 4.3,but works on 4.0
break;
case R.id.button_search_1:
dostuff2();//stuff2 is not happening in android 4.3,but works on 4.0
break;
......plenty of other stuff
}
I checked for the problem on plenty of places but could not find solution.My code works perfect on
devices with android 4.0 but in 4.3 nothing happens when i click on the image.
here is the xml-
.......
<ImageView
android:id="#+id/imageview_searchbar"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_below="#+id/rltv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/search_bar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageview_searchbar"
android:layout_alignLeft="#+id/imageview_searchbar"
android:layout_alignRight="#id/imageview_searchbar"
android:layout_alignTop="#+id/imageview_searchbar"
android:orientation="horizontal"
android:weightSum="21" >
<View
android:id="#+id/button_search_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
</LinearLayout>
.......lots of other stuff
I figured it out-I just had to bring the linear layout(containing buttons) to the front.
It got hidden under the image when using android 4.3 so i did this-
LinearLayout search_bar_buttons = (LinearLayout) view.findViewById(R.id.ll_search_bar_buttons);
search_bar_buttons.bringToFront();
Thank you guys for helping
I have an app which inflates button views on a click of a button(Button A). The button views inflated work properly, executing the code in their onClick method.
Now to save the layout generated, I keep a count of number of times the Button A is clicked and save this value using SharedPreferences. In onCreate method I create a for loop for the number of times the Button A was clicked and re-inflate those buttons, thus generating the same layout that was on screen when the app was killed. Though now, the buttons generated in the onCreate method no longer function.
I hope I have made some sense. The problem, when the app is started again in future, it builds the layout as it was when the app was clicked, but the button views no longer function for the onCLick method.
Here's the code for reference:
MainActivity.java
public class MainActivity extends Activity {
EditText et;
String z, r;
LinearLayout ll;
SharedPreferences sp;
Button fb1;
int c=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
sp=getSharedPreferences("MY_PREFS", 0);
if (sp.contains("counter")) {
for (int i=1;i<=sp.getInt("counter", 1);i++) {
cr();}
}
c=sp.getInt("counter", 0);
}
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
}
public void click(View v) {
int z=v.getId();
switch (z) {
case (R.id.button):
et=(EditText) findViewById(R.id.et);
cr();
et.setText("");
c=c+1;
break;
}
Editor e=sp.edit();
//e.putString("check", r);
e.putInt("counter", c);
//e.putInt("id", c);
e.commit();
for (int i=0; i<=c;i++){
if (z%2==0) {//Present
et.setText("Present");}
else if(z%2==1)
et.setText("Absent");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll"
tools:context=".MainActivity" >
<EditText
android:id="#+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Enter something" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:onClick="click"
android:text="Click" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove" />
</LinearLayout>
infla.xml
<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/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="click" />
</LinearLayout>
Edit:
You seem to have assigned redundant id's to the button during initialization.Each button would be assigned the value 'c' instead of 0 to 'c'.
Additionally It seems only logical to create click listeners on the fly as well ,since you are inflating button on the go.Try implementing the onclicklistener instead of the hardwired click() method in the xml.
i.e
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
fb1.setOnClickListener(new View.OnClickListener(){
//Stuff you wish to do on click
});
}
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 want to create an activity such as mentioned in photo...
as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, and again when I press the restore button I want it to become in a first state: to be able to see part 1 and part 2 ...
I think if we put two layouts it is possible? Isn't it? Please refer me to a resource to help me about this, or show me the code to achieve a solution.
Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.
Ok, here I go. Below you have a complete example of how to hide/show grouped views.
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
I hope this helps ;)
There is a bit simplified solution, than Diego Palomar produced, without using additional variable. I'll take his code to show:
public class MyActivity extends Activity implements OnClickListener {
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (mViewGroup.getVisibility() == View.VISIBLE) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
}
I have very simple code that includes ImageButton with OnClickListener that points to another Activity, the click on the ImageButton doesn't fire the onClick (The same problem was with Button) :
public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
this.btnCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tools, menu);
return true;
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/btncamera"
android:contentDescription="#string/desc" />
I can't see anything wrong with the code. If it works with a regular button my guess is that you maybe have to set android:clickable="true" in the xml (you can also do it in code).
I opened new Android project and copy paste may code, and it works, I guess something with the project was damaged.