I have six RadioButtons belonging to the same question, and I need to put them in some RadioGroup. But these RadioButtons is in some LinearLayouts. I tried to put one RadioGroup to each RadioButton, but i don't know what i need to do for it to work.
tab_layout_a.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/tipoPackRadio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Normal"
android:textSize="#dimen/radiobuttons_font_sz" />
<RadioButton
android:id="#+id/tipoPackRadio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Gilda"
android:textSize="#dimen/radiobuttons_font_sz" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.9"
android:orientation="horizontal">
<RadioButton
android:id="#+id/tipoPackRadio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Aged Pack \n(cheese/salve)"
android:textSize="#dimen/radiobuttons_font_sz" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/tipoPackRadio4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="0dp"
android:layout_weight="1"
android:text="Fellowship"
android:textSize="#dimen/radiobuttons_font_sz" />
<RadioButton
android:id="#+id/tipoPackRadio5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Fertilizer"
android:textSize="#dimen/radiobuttons_font_sz" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:orientation="horizontal">
<RadioButton
android:id="#+id/tipoPackRadio6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Aged Pack \n(honey)"
android:textSize="#dimen/radiobuttons_font_sz"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Try this solution..
class MyActivity extends Activity {
RadioButton _Rone, _Rtwo, _Rthree;
#Override
protected void onCreate(Bundle savedInstanceState) {
//after setContentView(.....)
_Rone = (RadioButton) findViewById(R.id.radio_one);
_Rtwo = (RadioButton) findViewById(R.id.radio_two);
_Rthree = (RadioButton) findViewById(R.id.radio_three);
_Rone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(true);
_Rtwo.setChecked(false);
_Rthree.setChecked(false);
}
});
_Rtwo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(false);
_Rtwo.setChecked(true);
_Rthree.setChecked(false);
}
});
_Rthree.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(false);
_Rtwo.setChecked(false);
_Rthree.setChecked(true);
}
});
// do code here
}`
by clicking on anyradio button the other's selection will reset.
Related
I implemented Bottom navigation bar in my project. When I declare and initialize buttons and then set onClickListener for them. The onclicklistener is not working fine without any error in logcat. I think its logical mistake that I'm unable to understand please see the code and guide me. (If you think the question is not according to community standards then I'm sorry in advance because I'm a beginner in java)
I searched a lot of questions related to this but nothing works for me.
Here is my main activity:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_notifications:
selectedFragment = new NotificationFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
Here is my First Fragment named HomeFragment:
public class HomeFragment extends Fragment implements View.OnClickListener {
Button btnFertilizers;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Logcat shows zero error.
I believe that here is the problem area. This is because the click function is not working properly for Fertilizers.class:
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Here is my FragmentHome.xml :
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/agriculture_home">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:src="#drawable/e_agriculture_logo"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Agriculture"
android:textColor="#ffffff"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="29sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/fertilizers"
android:layout_width="144dp"
android:layout_height="113dp"
android:layout_centerInParent="true"
android:src="#drawable/fertilizers_png" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fertilizers"
android:text="Fertilizers"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFertilizers"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/seeds"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/seed" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/seeds"
android:text="Seeds"
android:gravity="center"
android:textColor="#color/colorBlack"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_seeds"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/deseases"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/deseases" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/deseases"
android:text="Diseases"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_deseases"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewPesticides"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/pesticides" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewPesticides"
android:text="Pesticides"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnPesticides"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewWeatherForecast"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/weatherforecast" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewWeatherForecast"
android:text="Weather Forecast"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnWeatherForecast"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewArticles"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/video" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewArticles"
android:text="Videos"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnArticles"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewFeedback"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/feedback" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewFeedback"
android:text="Feedback"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFeedback"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewLocation"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/location_icon" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewLocation"
android:text="Location"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
tools:ignore="NotSibling" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnLocation"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here is my MainActivity.xml file:
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#color/colorWhite"
app:itemTextColor="#color/colorWhite"
android:background="#color/colorGreen"/>
</RelativeLayout>
How I should setOnClickListener to access my wanted activity?
Move this code into onCreateView.
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
remove this
implements View.OnClickListener
remove this
#Override
public void onClick(View view) { }
You are calling another onClick lisnter inside the onclick method so it will no work.You should have to change the code like this:
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnFertilizers) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
Return v in your onCreateViewMethod .
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return v;
}
I'm trying to make a visualization of some data in Android using a vertical bar chart, but when I increase or decrease the height of a View programatically by clicking a button, it changes from top to bottom, as in the image below (default value is 300).
How do I change the height from bottom to top, ie keeping the base of the view fixed?
Expected result:
My code in Java:
public final class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout layoutXAxis = (LinearLayout)this.findViewById(R.id.layoutX);
LinearLayout layoutYAxis = (LinearLayout)this.findViewById(R.id.layoutY);
LinearLayout layoutZAxis = (LinearLayout)this.findViewById(R.id.layoutZ);
TextView txtXAxis = (TextView)layoutXAxis.findViewById(R.id.txtXAxis);
TextView txtYAxis = (TextView)layoutYAxis.findViewById(R.id.txtYAxis);
TextView txtZAxis = (TextView)layoutZAxis.findViewById(R.id.txtZAxis);
View graphXAxis = (View)layoutXAxis.findViewById(R.id.XAxis);
View graphYAxis = (View)layoutYAxis.findViewById(R.id.YAxis);
View graphZAxis = (View)layoutZAxis.findViewById(R.id.ZAxis);
LinearLayout layoutBtnAdd = (LinearLayout)this.findViewById(R.id.layAdd);
Button btnAddX = (Button)layoutBtnAdd.findViewById(R.id.btnXAdd);
Button btnAddY = (Button)layoutBtnAdd.findViewById(R.id.btnYAdd);
Button btnAddZ = (Button)layoutBtnAdd.findViewById(R.id.btnZAdd);
LinearLayout layoutBtnSub = (LinearLayout)this.findViewById(R.id.laySub);
Button btnSubX = (Button)layoutBtnSub.findViewById(R.id.btnXSub);
Button btnSubY = (Button)layoutBtnSub.findViewById(R.id.btnYSub);
Button btnSubZ = (Button)layoutBtnSub.findViewById(R.id.btnZSub);
txtXAxis.setText(String.valueOf(graphXAxis.getLayoutParams().height));
txtYAxis.setText(String.valueOf(graphYAxis.getLayoutParams().height));
txtZAxis.setText(String.valueOf(graphZAxis.getLayoutParams().height));
btnAddX.setOnClickListener((OnClickListener)(new OnClickListener() {
public final void onClick(View it) {
View graph = graphXAxis;
LayoutParams params = graph.getLayoutParams();
params.height += 10;
graph = graphXAxis;
graph.setLayoutParams(params);
TextView text = txtXAxis;
View graph = graphXAxis;
text.setText(String.valueOf(graph.getLayoutParams().height));
}
}));
... // onClickListener of the other buttons
}
My code in XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/layoutY">
<TextView
android:id="#+id/txtYAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Y AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_blue_light"/>
...
<View android:id="#+id/YAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_blue_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutX"
android:layout_toLeftOf="#id/layoutY"
android:layout_toStartOf="#id/layoutY"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="#+id/txtXAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="X AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"/>
...
<View android:id="#+id/XAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutZ"
android:layout_toRightOf="#id/layoutY"
android:layout_toEndOf="#id/layoutY"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp">
<TextView
android:id="#+id/txtZAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Z AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_green_light"/>
...
<View android:id="#+id/ZAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
...
</RelativeLayout>
You might consider constructing your layout like the following.
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button_layout"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtYAxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Y AXIS"
android:textColor="#android:color/holo_blue_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/YAxis"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtXAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_red_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/XAxis"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutZ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtZAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Z AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_green_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/ZAxis"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the output.
Hope that helps!
So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.
Here is my swapFragment() method that calls my fragment:
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flgenre, genresFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Here is my GenresFragment.java:
public class GenresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private DatabaseReference mPostReference;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
private ArrayList<String> genreList = new ArrayList<>();
private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public GenresFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static GenresFragment newInstance(int columnCount) {
GenresFragment fragment = new GenresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);
if (rootView == null){
Log.e("TEST1", "Is Null");
}else {
Log.e("TEST2", "Not Null");
}
return rootView;
}
public void failure(){
Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(String item);
}
}
And my fragment_mini_add.xml:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
tools:context="com.nocrat.fanti.ProfileActivity"
android:id="#+id/flgenre">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="#string/add_new_project"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/rLayout1"
android:layout_below="#id/textView4">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/project_name"
android:gravity="center_vertical"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:hint="#string/group_name"
android:inputType="textPersonName"
android:layout_below="#id/editText"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_below="#id/rLayout1">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create"
android:textColor="#FFD700"
android:textSize="18sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:textColor="#FFD700"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here is my onClickListener that calls swapFragment():
addGenre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
swapFragment();
}
});
Here is my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:background="#ffffff"
android:orientation="vertical"
android:id="#+id/flgenre">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:elevation="40dp"
android:background="#ffffff">
<include
layout="#layout/app_bar_profile"
android:layout_height="190dp"
android:layout_width="match_parent"
android:id="#+id/app1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8musicalnotes64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Genres"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rounded_genres"
android:gravity="center"
android:clickable="true"
android:id="#+id/addGenres">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_add_white_48px" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8resume64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Bio" />
<EditText
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Tell us about yourself..."
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="edit"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8trophy64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Average Rank"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8meeting64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Collaborations"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.
Try to modify you swapFragment() with below code by replacing replace() with add()
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flgenre, genresFragment);
fragmentTransaction.commit();
}
Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.
Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.
Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)
Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.
You should remove android:id="#+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml.
replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml.
then, add this code to your activity xml to contain the fragment
<FrameLayout
android:id="#+id/flgenre"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I'm really new in coding for Android, just started to learn. I have some image buttons to change values displayed in textboxes and I don't know how to do this. I tried some tests for buttons but I have some errors which I can't repair.
Message from eclipse is:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
and
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
for setOnClickListener and OnClickListener
I looked at this thread but still I don't know what to do
setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);
Except that, I want to change the Medium text fields with the buttons. Perhaps by the declared integers.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
int vs,vh,ws,wh,wth,wts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener()
{
protected void onClick(View v)
{
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
}
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"
android:background="#012e53"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context="pl.tmalachowski.truewindcalc.MainActivity" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="26dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="vertical"
android:textColor="#bdc3c7" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView3"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float" />
</LinearLayout>
I think you are using your onClickListener wrong.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
Please try this usage. Hope it helps
Declare like this ImageButton b1; instead of Button b1;
then change to like this following code
b1=(ImageButton) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
I have a big issues about onSwiped method and set a button's visibility to VISIBLE. Here my code Java Class :
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
mRecyclerView.getAdapter();
StepListAdapter.ViewHolder listViewHolder = (StepListAdapter.ViewHolder) viewHolder;
listViewHolder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.accent));
listViewHolder.titleView.setVisibility(View.GONE);
listViewHolder.locationView.setVisibility(View.GONE);
listViewHolder.status.setVisibility(View.GONE);
listViewHolder.undoButton.setVisibility(View.VISIBLE);
listViewHolder.undoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
});
}
And here my xml layout file:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:clickable="true"
android:foreground="?selectableItemBackground"
card_view:cardCornerRadius="1sp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="5sp">
<RelativeLayout
android:background="#color/cardview_light_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_gravity="center"
android:padding="5sp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:text="#string/empty"/>
<TextView
android:id="#+id/location_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:ellipsize="end"
android:layout_gravity="center"
android:padding="5sp"
android:textColor="#color/dark_grey"
android:layout_below="#id/step_title_text"
android:text="#string/empty"/>
<ImageView
android:id="#+id/status_image"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<Button
android:id="#+id/undo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/undo"
android:textAllCaps="true"
android:visibility="gone"
android:textColor="#android:color/white"
android:layout_gravity="end|center_vertical"
android:layout_alignParentRight="true"
style="#style/Base.Widget.AppCompat.Button.Borderless"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
My problem is that when the method onSwiped was called, and after I setup the VISIBILITY of the button by Java code, the main card doesn't show the button .
Thanks a lot guys