So my layout is still visible, maybe someone sees mistake? I want to dispear it when infoButton(imagebutton) is clicked.
FrameLayout infolayout;
infolayout = (FrameLayout) findViewById(R.id.infoLayout);
public void infoPressed(View v){//info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
<FrameLayout
android:layout_width="193dp"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/serviceLayout"
android:id="#+id/infoLayout">
I wacthed: How to change visibility of layout programaticly
EDITED:
wrong code part, still not working for me
*Try this**
<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"
tools:context="com.example.demo.MainActivity" >
<FrameLayout
android:id="#+id/serviceLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#000000" />
<Button
android:id="#+id/btnHide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="infoPressed"
android:text="Hide" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
FrameLayout infolayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infolayout = (FrameLayout) findViewById(R.id.serviceLayout);
}
public void infoPressed(View v){ //info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
}
if your ID declaration is correct I guess you have to change the line
infolayout.setVisibility(View.INVISIBLE);
to
infolayout.setVisibility(FrameLayout.INVISIBLE);
Related
The following code is trivial but simulates the problem.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<Button
android:id="#+id/btnGoToExtra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="266dp"
android:text="Go To Extra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_extra.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<Button
android:id="#+id/btnBackToMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="186dp"
android:text="Back To Main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(R.layout.layout_extra);
findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(R.layout.activity_main);
});
});
}
}
Question
I can navigate to the layout_extra from activity_main and return to activity_main. Unfortunately, after this round trip, I can no longer be able to navigate to layout_extra anymore.
What causes this problem and how to fix it?
Explanation will follow in chat but that is not how it is supposed to be done, For views to change, You have other options like using Fragments or using new activity entirely, I would also like to suggest moving to kotlin.
public class MainActivity extends AppCompatActivity {
View mainView;
View extraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
extraView = getLayoutInflater().inflate(R.layout.layout_extra, null);
setContentView(mainView);
mainView.findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(extraView);
});
extraView.findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(mainView);
});
}
}
Reading through the snippet above, here's how everything went
The line
findViewById(R.id.btnGoToExtra)
.setOnClickListener
Registers Button with id btnGoToExtra for click events and when that happens you have the nested
findViewById(R.id.btnBackToMain)
.setOnClickListener
Which registers the element with id backToMain for click events and that's all
Calling setContentView() multiple times is not recommended. The normal way to switch between layouts in the same activity is to use a ViewFlipper or FrameLayout
From here
You then have:
XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
code
private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}
private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}
As you can see on the image below, I am having a hard time to figure out on how to deselect the first menu item since it is active by default on the BottomNavigationView.
I already tried everything but still the first menu is still raised.
Java Code.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigation);
bottomNavigationView.setBackground(null);
bottomNavigationView.getMenu().setGroupCheckable(0, false, true);
bottomNavigationView.setElevation(0);
//bottomNavigationView.setSelectedItemId(R.id.menu2);
}
}
Main XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/main_bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/main_bottom_menu"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="#color/_Surface"
app:tint="#color/_OnSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/dashboard_24"
app:layout_anchor="#+id/main_bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
If you have any ideas on how to normalize the menu items on the BottomNavigationView, I will really appreciate the help.
Thanks.
Try to add this in your bottomNavigationView in main.xml
app:labelVisibilityMode="unlabeled"
So your view will be like this:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/main_bottom_menu"
app:labelVisibilityMode="unlabeled"
/>
You can see more details about what changes you are allowed to make on view here:
https://material.io/components/bottom-navigation/android#using-bottom-navigation
public static void setCheckable(BottomNavigationView view, boolean checkable) {
final Menu menu = view.getMenu();
for(int i = 0; i < menu.size(); i++) { // Your Menu 0
menu.getItem(i).setCheckable(checkable);
}
}
Since the related menu is 0, you can arrange the code according to 0.
I have an EditText with some preinstalled text. When EditText was created, I want to select all text and the positioning at the beginning of the text. But selectAll() always transfers scrollPosition to the end.
EditText edt;
edt.requestFocus();
edt.selectAll();
edt.scrollTo(0,0); //not scrolling
Refer this example:
activity_main.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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="Enter name"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine" />
<EditText
android:id="#+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="Description"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine"
android:maxLines="6"
android:minLines="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Clickhandle"
android:text="Click" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.etEmail);
// editText.setSelectAllOnFocus(true); --> Select all text inside EditText when it gets focus
}
public void Clickhandle(View view) {
editText.requestFocus();
editText.selectAll();
editText.post(new Runnable() {
public void run() {
editText.scrollTo(0, 0);
}
});
}
}
I'm new to Android and recently created a dashboard for my app. I followed a tutorial but it only covered the front end. I created a layout called content_main.xml with three Cardviews. Each of these Cardviews contains the code
android:clickable="true"
Beginning the back end of things, I created a java receiver class called DashboardActivity (as seen below). I want to make my Cardviews clickable so that when one is clicked, I am taken to another screen within my app. The other screens in my app I am trying to get to are fragments. Each of these screens has its own java class in my "Fragments" folder. One of them, of which I try calling on in the code that follows is called SettingsViewFragment().
So far, I have tried the following. It does nothing.
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = (CardView) findViewById(R.id.dash_settings);
cd2 = (CardView) findViewById(R.id.dash_profile);
cd3 = (CardView) findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment1 = new SettingsViewFragment();
}
});
}
}
UPDATE: FULL CODE
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = findViewById(R.id.dash_settings);
cd2 = findViewById(R.id.dash_profile);
cd3 = findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Testing 123 123 123");
}
});
}
}
And my XML: (it continues for 3 cards this way)
<?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:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.newproject.DashboardActivity"
tools:showIn="#layout/toolbar">
<FrameLayout
android:id="#+id/dashframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/bg_circle1"
android:src="#drawable/ic_settingspicture"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="View your settings"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow along with guided tutorials"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
Replace
Fragment fragment1 = new SettingsViewFragment();
With this
Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Main_Layout,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Change Fragment fragment1 = new SettingsViewFragment (); by Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();
//if show toast The problem is not the onClick, the problem is when loading the fragment
Fragments should be instantiated with the newInstance() static method.
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment settingsFragment = SettingsFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.contentView, settingsFragment);
fragmentTransaction.commit();
}
});
In the SettingsFragment
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
XML for content_main
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<FrameLayout
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>
This will transact the fragment into the FrameLayout that is positioned below the CardViews. Now this is probably not ideal, you would most likely want to launch a new activity that will host the fragment but this should work. Unless this is already in some Activity that hosts the fragment. But the key code is in the onClick.
I have a problem with my first application,this is also my first question,i have 2 activity and 2 layout
this is the MainActivit.java
public class MainActivity extends TabActivity {
TabHost tabHost;
public static int meth;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadAllVar();
UpdateHud(meth);
TabSwitcher();
}
public static int LoadAllVar(){
//Load all var from files (not added for now)
meth = 200;
return meth;
}
private void UpdateHud(int meth){
String methstring = Integer.toString(meth);
TextView textSell = (TextView)findViewById(R.id.textMethCount);
textSell.setText(methstring);
}
public void TabSwitcher() {
tabHost = getTabHost();
TabHost.TabSpec tab1spec = tabHost.newTabSpec("Tab1");
tab1spec.setIndicator("Cook");
Intent firstintent = new Intent(this, CookTab.class);
tab1spec.setContent(firstintent);
tabHost.addTab(tab1spec);
}
}
and this is the second Activity
public class CookTab extends Activity {
public static int meth;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cooktab);
LoadVarFromCook();
CheckCookClick();
}
public void LoadVarFromCook (){
meth = LoadAllVar();
String methstring = Integer.toString(meth);
Toast.makeText(getApplicationContext(),methstring,Toast.LENGTH_SHORT).show();
}
public void CheckCookClick (){
Button buttoncook = (Button) findViewById(R.id.buttonCook);
buttoncook.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
meth = meth + 1;
Toast.makeText(getApplicationContext() , "+1" , Toast.LENGTH_SHORT).show();
//HERE I NEED TO UPDATE THE VALUE OF METH INSIDE THE ACTIVITY_MAIN.XML
}
});
}
}
this is the MainActivity layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TabHost
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"></TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent"></FrameLayout>
</TabHost>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:id="#+id/layoutHUD">
<TextView
android:layout_width="50dp"
android:layout_height="40dp"
android:id="#+id/textMethCount"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp" />
</RelativeLayout>
</RelativeLayout>
and this is the second tab layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/textMeth"
android:layout_marginTop="250dp"
android:layout_marginLeft="145dp"
android:text="Meth"
android:gravity="center"
android:textSize="29dp"
android:textStyle="bold"/>
<Button
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/buttonCook"
android:layout_below="#+id/textMeth"
android:layout_alignLeft="#+id/textMeth"
android:layout_alignStart="#+id/textMeth"
android:text="Cook"/>
</RelativeLayout>
I need to change the text inside a TextView located inside the activity_main.xml when i press a button inside the cooktab.xml how can i do that? findViewById() dosen't work
P.S. for now i add only one tab ,i will add more tab but for now i think about the first one
Try using EventBus it allows you to call functions in other parts of your app. Alternatively use a standard java interface link
I resolve the problem following this tutorial http://mrbool.com/how-to-create-an-activity-android-with-a-tabhost-view/27990