Show/Hide the Custom ListView in android - android

Can anybody guide me how can i Show/Hide the Custom ListView (BaseAdapter based) from my Activity.
For some more elaboration I have an activty which has a BUTTON and a LISTVIEW, I have an Inner Class MyAdapter extends with Base Adapter.
Now pressing a BUTTON on my ACTIVITY SHOW/HIDE the ListView.
CAn anybody guide.
Thanks

Just change the visibility of your ListView on the press of the button. using this line:
listView.setVisibility(View.VISIBLE);
To show.
and:
listView.setVisibility(View.GONE);
to hide.
you can also use: listView.setVisibility(View.INVISIBLE); to hide the listView but still take it's screen place.

You could try this. Using a toggle button:
public class ShowHideListViewActivity extends Activity {
ToggleButton tb;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylistviewlayout);
tb = (ToggleButton) findViewById(R.id.toggleButton1);
lv = (ListView) findViewById(R.id.listView1);
//You could set ListView Adapter here.
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
lv.setVisibility(View.VISIBLE);
}
else
{
//lv.setVisibility(View.GONE);
lv.setVisibility(View.INVISIBLE);
}
}
});
}
}
XML file:
<?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"
android:orientation="vertical" >
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textOn="Show"
android:textOff="Hide"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="406dp" >
</ListView>
</LinearLayout>

Related

How to change visibility of a textview which is in a listview

I have a listView in my activity_main.xml . I used a layout(list_layout) for my listview's row. list_layout contains 3 textView. I added a activity called "Setting" into my Mainactivity. I want change visibility of list_layout's 3. textView from settin.java with a button.
I mean when I click button (button code is into setting.java(button is into activity_setting.xml)) list_layout's 3.textview must invisible.
This is from activity_main.xml
<ListView
android:id="#+id/listem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
This is list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
.../>
<TextView
.../>
<TextView
android:id="#+id/turkish_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:visibility="visible"/>
</LinearLayout>
MainActivity.Java
... listview = (ListView) findViewById(R.id.listem);
DataHelper.Database data = new DataHelper.Database(MainActivity.this);
ArrayList<HashMap<String, String>> Liste = data.Listele();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, Liste, R.layout.list_layout, new String[]{"id", "title", "subtitle"}, new int[]{R.id.kelime_id, R.id.english_id, R.id.turkish_id});
listview.setAdapter(adapter);
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, Setting.class);
startActivity(intent);
break; ...
//Setting.Java
public class Setting extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
public void click(View view) {//<-----Here is my button's code
textView=(TextView)view.findViewById(R.id.turkish_id);
textView.setVisibility(View.INVISIBLE);
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
There are different methods for adding a click listener to a button , refer to this link:
add onclick listener to predefined button?
in your case you could implements in your activity the interface OnClickListener
public class Setting extends AppCompatActivity implements OnClickListener
then you should rename your method click to onClick
and in onCreate of your activity you should add the line
findViewById(R.id.yourIdButton).setOnClickListener(this);
don't forget to give an id to your button
<Button
android:"#+id/yourIdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
you can also remove completely the TextView from layout using "View.GONE" in place of "View.INVISIBLE"

How to disable area of Gridview before pressed a Button?

I'm trying to make a simple game that select a value from item of Gridview. But i want that value just can clickable after press Play Button.We can't click in anywhere if did not press Play Button. How can i do that?
What you can do is to change the clickable property of your grid view
and then in the button click listener just set the clickable property to true
so your code should look like the following:
let's assume that this is your view:
<?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"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="50dp" />
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" />
</LinearLayout>
your activity should contain the following code:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private GridView mGridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.grid);
mButton = (Button) findViewById(R.id.button);
// if you want to do it with setEnable
mGridView.setEnabled(false);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGridView.setClickable(true);
// if you want to toggle then you can use the following code:
// mGridView.setClickable(!mGridView.isClickable());
// you can also change the setEnable property and not the clickable
mGridView.setEnabled(true);
}
});
}
}

how to get modified content as android layout xml file?

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.

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.

How to create radio button without knowing exactly count?

How to create radio button in radioGroup in file layout without knowing exactly count
Dose anyone has any ideas?
I request you to say thing clearly,but as i got ,you want many number of radio Button,
Take a loop and create dynamic RadioButton add these button to Radio Group
May be this is of any help;
public class RadDemoActivity extends Activity {
private RadioGroup radGroup;
private Button addButton;
private RadioButton radButton;
private static int no;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
no=0;
doInflateItems();
setAddButton();
}
private void doInflateItems() {
radGroup = (RadioGroup) findViewById(R.id.radGroup);
addButton = (Button) findViewById(R.id.addRad);
}
private void setAddButton() {
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
radButton = new RadioButton(getApplicationContext());
radButton.setText("Option " + no++);
radGroup.addView(radButton);
}
});
}
}
XML 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">
<Button android:id="#+id/addRad" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add" />
<ScrollView android:id="#+id/vScroll" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radGroup"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
This code is adding radio buttons from a button click, you can do the same from a loop if you want to add multiple buttons at once. Hope this helps.

Categories

Resources