Changing layouts error? - android

Trying to change a layout from an ImageButton but when i run it on my phone it does not switch instead it says program has stopped.
Java;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ImageButton = (ImageButton) findViewById(R.id.image);
ImageButton.setBackgroundColor(1);
ImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
setContentView(R.id.aaa);
}
});
}
}
XML for first layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.bassammetwally.anew.MainActivity">
<RelativeLayout
android:layout_width="420dp"
android:layout_height="50dp"
android:background="#3955a1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writer's Block"
android:textColor="#ffffff"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:id="#+id/Asd"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="40dp"
android:src="#drawable/pen"
android:layout_marginLeft="380dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/image"/>
</RelativeLayout>
</LinearLayout>
Second layout to change to;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/aaa">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans"/>
</LinearLayout>
I think it might have something to do with my syntax.

You need to use a layout reference instead of an id
setContentView(R.layout.view);
Where view.xml is the layout you want.
You may want to rethink your approach, though. Using Fragments is preferred over completely replacing the layout because your ImageButton is no longer usable and you'll need to recall findViewById for any new views that are displayed.
If you just want to load a layout file and add it to the current layout, see Inflating one layout

Related

Android studio switch layouts using buttons

My question its very simple, how can I switch between layouts using buttons in each layouts?
Layout1:
<?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/button"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="7dp"
android:layout_marginRight="7dp"
app:backgroundTint="#FFFFFF"
app:icon="#drawable/ic_polygon"
app:iconTint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout2:
<?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:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:text="Back"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The buttons:
btn_1= findViewById(R.id.button);
btn_2= findViewById(R.id.button1);
btn_1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.layout2);
}
});
btn_2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.layout1);
}
});
The first button works fine, but the seconds doesnt.
I have tried inflate the view as well:
view_2 = getLayoutInflater().inflate(R.layout.layout2, null);
btn_2 = view_2.findById(R.id.button2);
I need to add more details, so, here I am.
I am want to switch layout using buttons, indeed the first button change the layout, but when I already change the layout I cannot use the other button to "return" to the first layout. Its suppose to look like a menu.
after many tests, it is best to use another application or fragments, where you can create another layout and change it without problems.

Android Wear: Card Scroll View Does Not Scroll

I am today testing out the card scroll view but with all the examples I followed my one does not scroll and I am not sure why.
card_scroll_view_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<android.support.wearable.view.CardScrollView
android:id="#+id/card_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_box="all">
<android.support.wearable.view.CardFrame
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/big_text"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
So this contains a card frame with a linear layout that holds two child text views. The last text view holds a really big string to test for scrolling
WearActivity.java
public class WearActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_scroll_view_test);
setupCardScrollView();
}
private void setupCardScrollView(){
CardScrollView cardScrollView =
(CardScrollView) findViewById(R.id.card_scroll_view);
cardScrollView.setExpansionEnabled(true);
cardScrollView.setExpansionDirection(CardFrame.EXPAND_DOWN);
cardScrollView.setCardGravity(Gravity.BOTTOM);
}
}
I also checked to see if the root box inset layout might have been the issue by making the CardScrollView the root view, but the problem persists.
It seems to work if you:
Remove BoxInsetLayout (CardScrollView become parent view)
Set setExpansionFactor on CardScrollView and set the properties on CardFrame as well.
Update to your code:
private void setupCardScrollView(){
CardScrollView cardScrollView =
(CardScrollView) view.findViewById(R.id.card_scroll_view);
cardScrollView.setExpansionEnabled(true);
cardScrollView.setExpansionDirection(1);
cardScrollView.setExpansionFactor(10.0F);
CardFrame cardFrame = (CardFrame) view.findViewById(R.id.card_frame);
cardFrame.setExpansionEnabled(true);
cardFrame.setExpansionDirection(1);
cardFrame.setExpansionFactor(10.0F);
}

Android List Below Toggle Buttons

I have a list that is intended to be below toggle buttons. The list grabs data from a server and then parses them. My XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/toggle_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textOff="Apps"
android:textOn="Apps" />
<ToggleButton
android:id="#+id/toggle_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/toggle_button1"
android:layout_weight="1"
android:textOff="VMs"
android:textOn="VMs" />
<ToggleButton
android:id="#+id/toggle_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/toggle_button2"
android:layout_weight="1"
android:textOff="Groups"
android:textOn="Groups" />
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toggle_button1" />
</RelativeLayout>
Code for the actual fragment:
public class ProblemFragment extends SherlockListFragment
{
private SeparatedListAdapter list;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getSherlockActivity().setContentView(R.layout.problem_layout);
list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
ToggleButton b1 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button1);
ToggleButton b2 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button2);
ToggleButton b3 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button3);
setListAdapter(list);
refresh();
}
public void refresh()
{
list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
refreshStats();
}
public void refreshStats()
{
//Omitted parsing code
list.addSection(new String("Hello world!!"));
setListAdapter(list);
}
}
However, when I use setListAdapter(list), the buttons are overwritten. They are visible before the app retrieves the data and parses it, but they are overwritten after I call setListAdapter. How can i fix this?
First, remove
android:orientation="horizontal"
from your root layout. RelativeLayout doesn't have an orientation property. Also, weight is for child elements of a LinearLayout and when you use it then you should assign the width of each child view to 0dp for horizontal orientation and height="0dp" for vertical orientation.
Then wrap your ToggleButtons in a LinearLayout, vertical or horizontal orientation, and give it the property
android:layout_alignParentTop="true"
then give your ListView the property
android:layout_below="#id/idOfLinearLayout"
So it may look something like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/toggleLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ToggleButton
android:id="#+id/toggle_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Apps"
android:textOn="Apps" />
<ToggleButton
android:id="#+id/toggle_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="VMs"
android:textOn="VMs" />
<ToggleButton
android:id="#+id/toggle_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Groups"
android:textOn="Groups" />
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toggleLL" />
</RelativeLayout>
I also removed the RelativeLayout properties from the ToggleButtons since they are now wrapped in a LinearLayout. And you had a circular view error there with assigning the second ToggleButton to the right of itself which may have been a copy/paste error. Hope this helps.
Note that the default orientation for a LinearLayout is horizontal so leaving that property out will give you that effect.
Oh! I can not test your XML but I think that you need scrollbars! If the list is filled with a lot of entries, it can became bigger that the screen, making the buttons disappear because they are pushed up by the list. Try to add a scroll to the whole layout.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Original layout here -->
</ScrollView>
</LinearLayout>
Of course, if you just put only one layout inside the scrollview, there is no need for the outer layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Original layout here -->
</ScrollView>

Layout Inflater in Android

There are 3 xml files. Main.xml has a linear layout.
Button.xml just contains a button.
txt.xml just contains an edit text.
I inflate button.xml to Main.xml.
Looks fine.
Button has the same size as it was in Button.xml.
Next i inflate edit text to Main.xml which has an ems=10(android:ems="10").
When i run, the button changes its size to same as that of edit text.
How does that happen and how to overcome the problem. the code is pasted.
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:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
tools:context=".First" />
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
Button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
txt.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
>
<requestFocus />
</EditText>
Main.java
public class First extends Activity {
LinearLayout lout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
final LayoutInflater inf=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button btn=(Button)inf.inflate(R.layout.button, null);
lout=(LinearLayout)findViewById(R.id.ll);
lout.addView(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(lout.getChildAt(5)==null){
EditText e=(EditText)inf.inflate(R.layout.txt, null);
lout.addView(e);
}
}
});
}
on 5 clicks of the button, 5 edit texts will show up. but the moment i click the button the size of the button changes in addition to the edit text formation.
Please paste your code here, only then anybody can help you. Still you can set a layout_height & layout_width of the button to something like 20sp(as per your need).
In your Main.xml file you need to fix the width of the layout as the layout is small at first because the textview is small. But when you click on the Button the layout increases as per the required width for the EditText and so the Button size also increases:
<LinearLayout android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="100sp"
android:layout_height="wrap_content" >
</LinearLayout>

Visibility of ScrollView

I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>
you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView
First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.

Categories

Resources