how to get modified content as android layout xml file? - android

I have implemented an application for modify the xml layout with some properties.
I have designed an xml file at R.layout.main.
I am setting Visibility property from my activity class.When i update main.xml file at run time i would like to get copy of main.xml file with updated property as Visibility as gone
Initially the main.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
//Here i did not set the android:visibility="gone"
//I would like to change the above property from activity class
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question1" android:textColor="#ffffff"/>
<CheckBox
android:id="#+id/questioncheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editTextone"
android:layout_width="180dip"
android:layout_height="wrap_content" android:focusable="true">
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
I have implemented my application as follows:
public class ContentExmpleActivity extends Activity {
LinearLayout l1,l2;
CheckBox c1,c2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1 =((LinearLayout)findViewById(R.id.linearLayout1));
l2=((LinearLayout)findViewById(R.id.linearLayout2));
c1=((CheckBox)findViewById(R.id.checkBox1));
c2=((CheckBox)findViewById(R.id.checkBox2));
c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l1.setVisibility(View.GONE);
}
});
c2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
l2.setVisibility(View.GONE);
}
});
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Implementation for get the modified content xml layout with properties from emulator
}
});
}
}
from the above class i am hidding layout when user check the check box then it is hidding.When user click on save button i would like to get the contentView xml file as android layout xml.i mean copy of the main.xml with modified properties as hiding layout properties.
How can i get copy of main.xml file when the content has modified?
please any body help me.....

Replace
setContentView(R.layout.main);
by
View vi = View.inflate(this, R.layout.main, null);
setContentView(vi);
Now, you have a reference of your modified view and you can assign it where you want.

Related

Android Studio Activity Layout Changing

I need help in changing layout of Activity in Android Studio when I click on button.
But I don't want to open new activity I want to change layout and design completely on the current activity by clicking button. Is it possible ?
you can inflate another layout during button click:
public class LayoutInflateDemo extends Activity {
private LinearLayout layoutToAdd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_existed);
layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.layout_toinfliate, null);
//Add the inflated layout to your existing view
layoutToAdd.addView(view);
}
});
}
}
layout to inflate Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/mytext"
android:text="Inflated layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And this is your existing layout:
<?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:id="#+id/existedlayout"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clicck to inflate view"
android:id="#+id/button"
/>
</LinearLayout>
Yes it is possible by using setContentView() method. But you have to make sure that you didn't use any view (e.g. view by id) that is not available on current layout.
Best way is Fragments and FragmentActivity https://developer.android.com/guide/components/fragments.html

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

How to custom ImageButton's image?

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);
}
});

make a frame with components visible only when needed in android

Well what i wanted to do was create an initial layout which will have a toggle button and on clicking the toggle but it should make a frame visible which will have a few buttons or textviews.
Does anybody know how to do this in Android 2.2??
You can use the visibility attribute on a view to control whether it is visible or not. Here's a small example that should do what you're looking for.
The main Activity:
public class DynamicLayoutTestActivity extends Activity {
private ToggleButton toggleButton;
private View possiblyHiddenView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
possiblyHiddenView = (View) findViewById(R.id.possiblyHiddenView);
toggleButton.setOnCheckedChangeListener(toggleButtonOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener toggleButtonOnCheckedChangeListener
= new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
possiblyHiddenView.setVisibility(View.VISIBLE);
} else {
possiblyHiddenView.setVisibility(View.INVISIBLE);
}
}
};
}
The layout file, 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"
>
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Show"
android:textOn="Hide" />
<LinearLayout
android:id="#+id/possiblyHiddenView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
>
<TextView
android:text="Stuff that could be hidden."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
If you don't want the hidden view to take up any space, use visibility gone instead of invisible.
Hope this helps!
If you're developing for Android 3.0+, look into fragments.

first click on linearlayout doesn flash background click color, it just flash for second and next click

I try to get the effect click background color for linear layout. I've set clickable to linear layout. and from the code also I've put the click listener the setBackgroundResource.
Here it is the 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"
>
<LinearLayout
android:id="#+id/llinsertmem"
android:clickable="true"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50px">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PUSH it"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
and the java code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
When first time click the clickable linearlayout, the toast text is displayed but the background color click effect doesn't. The flash background click color is only work from the second click.
any idea what the problem is?
Is not necessary do anything in JAVA code.
You can only add this as attribute:
android:background="#android:drawable/list_selector_background"
And it works for me (on Android 2.2 device)
After try and error, somehow it's work.
just put the setBackgroundResource also on the onCreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
Don't know the logic explanation. if you have a thought, please.
In the onClick method you are using v as the View which is passed, but that view may not be the LinearLayout which you want to change the background.
Thus you need to create a class level variable or a final varibale and pass the handle for the LineraLayout to the onClick method.
Remove/cut from onCreate:
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
and paste it into onClick or change v. into linearInsertMem.
I think that Exlipse will then demand that linearInsertMem must be final like:
final LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
Or you can define this object above onCreate like this:
LinearLayout linearInsertMem;
then in onCreate you state:
linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
then onClick method will know exactly which view you want to change if you use linearInsertMem.setBackgroundResource...

Categories

Resources